<template>
	<view class="">
		<!-- 灭火器安全检查记录表 -->
		<view class="files">
			<view class="tabs">
				<fui-list bottomBorder title="巡查人员基本信息">
					<view class="tab">
						<fui-list-cell>
							<text>巡查人员</text>
							<text class="fui-text__explain">{{inspectionStaff.name}}</text>
						</fui-list-cell>

						<fui-list-cell>
							<text>巡查日期</text>
							<text class="fui-text__explain">{{inspectionStaff.data}}</text>
						</fui-list-cell>

					</view>
				</fui-list>

				<fui-list bottomBorder title="灭火器安全检查登记">
					<view class="tab">
						<fui-input required @click="showOpsOne =!showOpsOne" v-model="situation" :label="month" readonly
							textAlign="right" placeholder="请选择"></fui-input>
						<fui-input @longpress="showColumns=!showColumns" required v-model="model" label="型号"  textAlign="right"
							placeholder="请输入"></fui-input>
						<fui-input required v-model="department" label="管理部门"  textAlign="right"
							placeholder="请输入"></fui-input>
						<fui-input required v-model="serialnumber" label="编号"  textAlign="right"
							placeholder="请输入"></fui-input>
					</view>
				</fui-list>

			</view>

			<view class="tab_ops">
				<!-- 	<view style="width: 100%; display: flex; justify-content: space-between;">
					<fui-button btnSize="small" >巡查人员签字</fui-button>
					<fui-button btnSize="small"  >安全主管签字</fui-button>
				</view> -->
				<view style="padding-top: 40rpx;">
					<fui-button @click="submit" type="success">提交</fui-button>
				</view>
			</view>
			<view>
				<u-picker @confirm="onOpsOne" @cancel="showOpsOne = !showOpsOne" :show="showOpsOne"
					:columns="columnsOne"></u-picker>
			</view>

		</view>
		<view >
			<up-picker @confirm="onColumns" @cancel="showColumns = !showColumns" :show="showColumns" :columns="columns"></up-picker>
		</view>
	</view>
</template>

<script>
	import moment, {
		duration
	} from "moment"
	import {
		BASE_IP
	} from '@/common/http/url.js'
	// import test from "../../../uni_modules/uview-plus/libs/function/test"
	// import text from "../../../uni_modules/uview-plus/libs/config/props/text"
	import tools from "@/tools/tools.js"
	
	export default {
		name: "fireExtinguishing",
		data() {
			return {
				showColumns : false,
				text: "次",
				user: "",
				inspectionStaff: {
					name: "",
					data: "",
					time: ""
				},
				situation: "正常",
				columnsOne: [
					['正常', '清洁', '异常']
				],
				showOpsOne: false,
				regime: "",
				year: "",
				month: "",
				day: "",
				model: "",
				department: "",
				serialnumber: "",
				isSubmit : false,
				columns :[[]],
				preset: ""
			}
		},
		methods: {
			onColumns(res){
				console.log(res)
				this.model = res.value[0]
				this.showColumns =!this.showColumns
			},
			/**
			 * 
			 * @param {Object} path 转入要签字的路径
			 */
			onSignatures(path) {
				let user = uni.getStorageSync("user")
				if (user.signature == "" || user.signature == undefined || user.signature == null) {
					uni.showModal({
						title: "提示",
						content: "用户未上传签名请前往“预设功能-添加操作员-输入账号-添加签名”，如果已添加操作员上传签名即可",
						showCancel: false,
						success: (res) => {
							if (res.confirm) {
								return
							}
						}
					})
					return
				}
				let docPath = path
				let url = BASE_IP + user.signature
				tools.instImgPdf(path,user.signature,420,350)
				// console.log(url)
				// let _this = this
				// uni.downloadFile({ //下载
				// 	url: url, //图片下载地址
				// 	success: res => {
				// 		if (res.statusCode === 200) {
				// 			uni.saveFile({
				// 				tempFilePath: res.tempFilePath,
				// 				success: (r) => {
				// 					console.log(r)
				// 					_this.upSignatures(r.savedFilePath, docPath)
				// 				}
				// 			})
				// 		}
				// 	}
				// });
			},
			/** 签字函数
			 * @param {Object} filePath 签字图片
			 * @param {Object} docPath  要签字的文档
			 */
			upSignatures(filePath, docPath) {
				console.log(filePath, docPath)
				let user = uni.getStorageSync("user")
				let url = BASE_IP + "pc/regulatory/signature/word"
				uni.uploadFile({
					header: {
						"Authorization": user.token,
					},
					name: "file",
					url: url,
					formData: {
						"unitId": user.unitId,
						"path": docPath
					},
					filePath: filePath,
					success: (res) => {
						if (res.statusCode == 500) {
							uni.showToast({
								title: "上传失败",
								icon: "error"
							})
							return
						}
					},
					fail: (res) => {
						uni.showToast({
							title: "上传失败,请重试",
							icon: "error"
						})
					}
				})
			},
			/**
			 * 盖章
			 * @param {Object} path 转入要盖章的文件路径
			 */
			onSeal(path) {
				let pralldata = uni.getStorageSync("pralldata")
				// console.log(pralldata)
				let docPath = path
				if (pralldata.unitFireSeal == "" || pralldata.unitFireSeal == undefined) {
					uni.showModal({
						title: "提示",
						content: "单位未上传消防专用章",
						showCancel: false,
						success: (res) => {
							if (res.confirm) {
								return
							}
						}
					})
					return
				}
				let url = BASE_IP + pralldata.unitFireSeal
				tools.instImgPdf(path, pralldata.unitFireSeal,100,350)
				// console.log(url)
				// uni.downloadFile({ //下载
				// 	url: url, //图片下载地址
				// 	success: res => {
				// 		if (res.statusCode === 200) {
				// 			uni.saveFile({
				// 				tempFilePath: res.tempFilePath,
				// 				success: (r) => {
				// 					// console.log(r)
				// 					this.upLoadSeal(r.savedFilePath, docPath)
				// 				}
				// 			})
				// 		}
				// 	}
				// });
			},
			/** 盖章函数
			 * @param {Object} filePath 印章图片
			 * @param {Object} docPath  要盖章的文档
			 */
			upLoadSeal(filePath, docPath) {
				let user = uni.getStorageSync("user")
				let url = BASE_IP + "pc/regulatory/update/word"
				uni.uploadFile({
					header: {
						"Authorization": user.token,
					},
					name: "file",
					url: url,
					formData: {
						"unitId": user.unitId,
						"path": docPath
					},
					filePath: filePath,
					success: (res) => {
						// console.log(res)
						if (res.statusCode == 500) {
							uni.showToast({
								title: "盖章失败",
								icon: "error"
							})
							return
						}
						if (res.statusCode == 404) {
							uni.showToast({
								title: "盖章失败",
								icon: "error"
							})
							return
						}
					},
					fail: (res) => {
						uni.showToast({
							title: "盖章失败,请重试",
							icon: "error"
						})
					}
				})
			},
			onOpsOne(res) {
				this.situation = res.value[0]
				// console.log(res)
				this.showOpsOne = !this.showOpsOne
			},
			submit() {
				if(this.isSubmit){
					uni.showToast({
						title:"请勿重复提交",
						icon:"error"
					})
					return
				}

				
				
				if(this.situation == ""){
					uni.showToast({
						title:"表单填写不完整",
						icon: "error"
					})
					return
				}
				if(this.model == ""){
					uni.showToast({
						title:"表单填写不完整",
						icon: "error"
					})
					return
				}
				if(this.department == ""){
					uni.showToast({
						title:"表单填写不完整",
						icon: "error"
					})
					return
				}
				
				if(this.serialnumber == ""){
					uni.showToast({
						title:"表单填写不完整",
						icon: "error"
					})
					return
				}
				
				let url = BASE_IP + "pc/record/record/fire/fireExtinguisherSafetyInspectionRecordForm"

				let classifyId = this.regime.classifyId
				// console.log(this.regime)
				let user = uni.getStorageSync("user")
				
				let json1 = {
					name  : this.inspectionStaff.name,
					data :this.inspectionStaff.data,
					v1 : this.model,
					v2 : this.department,
					v3 : this.serialnumber,
					v4 : this.month,
					v5 : this.situation
				}
				let flag_storage = true
				// let user = uni.getStorageSync("user")
				if (user.signature == "" || user.signature == undefined || user.signature == null) {
					uni.showModal({
						title: "提示",
						content: "用户未上传签名请前往“预设功能-添加操作员-输入账号-添加签名”，如果已添加操作员上传签名即可",
						showCancel: false,
						success: (res) => {
							if (res.confirm) {
								flag_storage = false
								uni.navigateTo({
									url: "/pages/preinstall/preinstall"
								})
								return
							}
						}
					})
					return
				}
				
				if (!flag_storage) {
					return
				}
				
				let pralldata = this.preset
				console.log(pralldata)
				let falg_seal = true
				if (pralldata.unitFireSeal == "" || pralldata.unitFireSeal == undefined) {
					uni.showModal({
						title: "提示",
						content: "单位未上传消防专用章,请前往“预设功能-单位消防专用章”上传",
						showCancel: false,
						success: (res) => {
							if (res.confirm) {
								falg_seal = false
								uni.navigateTo({
									url: "/pages/preinstall/preinstall"
								})
								return
							}
						}
					})
					return
				}
				
				if (!falg_seal) {
					return
				}
				uni.showLoading({
					title:"正在提交"
				})
				this.isSubmit =!this.isSubmit
				uni.request({
					url:url,
					header: {
						"Authorization": user.token,
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					data:{
						unitId: user.unitId,
						name: user.nickName,
						classifyId: classifyId,
						json : JSON.stringify(json1),
						uuid : user.uuid
					},
					method:"POST",
					success: (res) => {
						console.log(res)

						if(res.statusCode == 200){
							uni.showToast({
								title:"上传成功",
								icon:"success"
							})
							// this.onSeal(res.data.path)
							// this.onSignatures(res.data.path)
							setTimeout((r)=>{
								uni.navigateBack({
								    delta: 1,
								    success: function () {
								        const pages = getCurrentPages(); //获取当前页面栈
								        const prevPage = pages[pages.length - 1]; //获取上一个页面实例对象
								        prevPage.getCollapseDatas(); //调用上一个页面的onLoad方法
								    }
								});
							},1500)
							return
						}

						if(res.statusCode == 500){
							uni.showToast({
								title:"上传失败",
								icon:"error"
							})
							this.isSubmit =!this.isSubmit
							return
						}
						if(res.statusCode == 404){
							uni.showToast({
								title:"上传失败",
								icon:"error"
							})
							this.isSubmit =!this.isSubmit
							return
						}
					},fail: (res) => {
						uni.showToast({
							title:"上传失败",
							icon:"error"
						})
						this.isSubmit =!this.isSubmit
					}
				})
			},
			getPrall() { //获取预设功能对象数据
			let pralldata = uni.getStorageSync("pralldata")
			this.preset = pralldata
				// let user = uni.getStorageSync("user")
				// let url = BASE_IP + "pc/presetfunction/find/presetFunction/" + user.unitId + "/" + user.uuid
				// uni.request({
				// 	header: {
				// 		"Authorization": user.token,
				// 	},
				// 	url: url,
				// 	success: (res) => {
				// 		this.preset = res.data
				// 	},
				// 	fail: (res) => {
				// 		// console.log("请求失败：", res)
				// 	}
				// })
			}
		},
		onLoad(ops) {
			uni.setNavigationBarTitle({
				title: "灭火器安全检查记录表"
			})

			let user = uni.getStorageSync("user")
			this.getPrall()
			console.log(user)
			this.regime = JSON.parse(ops.regime)
			console.log(this.regime)
			this.inspectionStaff.name = user.nickName

			const currentDate = moment();
			const year = currentDate.year();
			const month = currentDate.month() + 1;
			const day = currentDate.date();
			this.year = year
			this.month = month + "月"
			this.day = day
			this.inspectionStaff.data = year + "-" + month + "-" + day

			let pralldata = uni.getStorageSync("pralldata")

			// let chartson = uni.getStorageSync("chartson")
			// console.log(chartson)
			// this.fireAlarm.fireAlarm = chartson.temp2.data[0]
			// this.fireAlarm.misstatement = chartson.temp3.data[0]
			// this.fireAlarm.faultAlarm = chartson.temp4.data[0]
			// this.fireAlarm.supervisionAlarm = chartson.temp5.data[0]

			this.inspectionStaff.time = pralldata.operatingTimeStart + " - " + pralldata.operatingTimeEnd
			
			let recordTable = uni.getStorageSync("recordTable")
			console.log(recordTable)
			recordTable.t1 = JSON.parse(recordTable.t1)
			for(let index in recordTable.t1){
				this.columns[0].push(recordTable.t1[index])
			}
			this.model = recordTable.t1[0]
			this.department = recordTable.t2
			this.serialnumber = recordTable.t3
			

		}
	}
</script>

<style>
	page {
		background-color: #F1F4FA;
	}

	.tabs {
		width: 95%;
		margin: auto;
	}

	.tab {
		width: 100%;
		background-color: white;
		border-radius: 8px;
		/* margin: auto; */
		box-shadow: 1px 1px 10px #DCDFE6;
		padding-top: 10rpx;
		padding-bottom: 20rpx;
	}

	.fui-text__explain {
		font-size: 14px;
		color: #606266;
	}

	.tab_ops {
		padding: 40rpx;
		padding: 40rpx;
	}
</style>